<?php
  include_once dirname(__FILE__)."/../database/database.class.php";
  include_once dirname(__FILE__)."/../database/card.database.class.php";
  include_once dirname(__FILE__)."/../classes/collection.class.php";
  include_once dirname(__FILE__)."/../classes/deck.class.php";
  include_once dirname(__FILE__)."/../includes/print_images.inc.php";

  class Card
  {
    private $id;

    public function Card($id)
    {
      $this->id = $id;
    }

    public function getCardname($cardside = 1)
    {
      global $cdb;
      $card = $cdb->getCard($this->id, $cardside);
      foreach($card as $subcard)
      {
        if($subcard->cardside == $cardside)
        {
          return $subcard->name;
        }
      }
      return "";
    }

    public function getID()
    {
      return $this->id;
    }

    public function getExternalID($type, $side = 1)
    {
      global $cdb;

      switch($type)
      {
        case("multiverseid"):
          return $cdb->getMultiverseID($this->id, $side);
        default:
          return 0;
      }
    }

    public function getTotalCards()
    {
      global $db;

      return $total = $db->getTotalCardCount($this->id, $_COOKIE["MGTDatabase_user"]);
    }

    public function getExpansion($format = "name")
    {
      global $db;

      switch($format)
      {
        case("internalname"):
          return $db->getExpansionOfCard($this->id)->internal_name;
        case("id"):
          return $db->getExpansionOfCard($this->id)->ID;
        default:
          return $db->getExpansionOfCard($this->id)->name;
      }
    }

    public function getCardDistribution()
    {
      global $db;

      $distro_obj = new stdClass();
      $distro_obj->collections = array();
      $distro_obj->decks = array();

      if(!isset($_COOKIE["MGTDatabase_user"]) || !is_numeric($_COOKIE["MGTDatabase_user"]))
      {
        return;
      }

      $collections = $db->getCollections($_COOKIE["MGTDatabase_user"]);
      $decks = $db->getDecks($_COOKIE["MGTDatabase_user"]);

      if(!empty($collections))
      {
        foreach($collections as $collection)
        {
          array_push($distro_obj->collections, new Collection($collection->ID));
        }
      }
      if(!empty($decks))
      {
        foreach($decks as $deck)
        {
          array_push($distro_obj->decks, new Deck($deck->ID));
        }
      }

      $distro_obj->total = $this->getTotalCards();
      $distro_obj->cardid = $this->id;
      return $distro_obj;
    }

    public function getDetails()
    {
      global $cdb;
      
      $details = new stdClass();
      $details->details = $cdb->getCardDetails($this->id);
      $details->card_text = $cdb->getCardText($this->id);
      return $details;
    }

    public function getConvertedManaCost()
    {
      return $this->getDetails()->details[0]->Converted_Mana_Cost;
    }
    
    public function getManaCostSymbols()
    {
      $manacost_distro = array();
      $manacost = $this->getDetails()->details[0]->Mana_Cost;
      
      $manacost_distro['white'] = substr_count($manacost, "White");
      $manacost_distro['blue'] = substr_count($manacost, "Blue");
      $manacost_distro['black'] = substr_count($manacost, "Black");
      $manacost_distro['red'] = substr_count($manacost, "Red");
      $manacost_distro['green'] = substr_count($manacost, "Green");

      return $manacost_distro;
    }

    public function getType()
    {
      $type = $this->getDetails()->details[0]->Types;

      if($result = strstr($type, "&#8208;", TRUE))
      {
        $type = trim($result);
      }

      if($result = strstr($type, "Legendary", FALSE))
      {
        $type = trim(substr($result, strlen("Legendary")));
      }

      if($result = strstr($type, "Basic", FALSE))
      {
        $type = trim(substr($result, strlen("Basic")));
      }

      if($result = strstr($type, "Snow", FALSE))
      {
        $type = trim(substr($result, strlen("Snow")));
      }

      return $type;
    }

    public function getImageURL($cardside = 1)
    {
      global $db, $cdb;

      $ending = ".jpg";
      $multiverseid = $this->getExternalID("multiverseid", $cardside);
      if($multiverseid != 0)
      {
        $ending = "_".$multiverseid.$ending;
      }

      $cardname = $this->getCardname($cardside);
      
      $cardname=str_replace('&eacute;','é',$cardname);
      $cardname=str_replace('&ouml;','ö',$cardname);
      $cardname=str_replace('&#198;','Ae',$cardname);
      $cardname=str_replace('&ucirc;','û',$cardname);
      $cardname=str_replace('&aacute;','á',$cardname);
      $cardname=str_replace(':','_',$cardname);
      $cardname=str_replace('//','||',$cardname);
      $cardname=str_replace(' ','_',$cardname);
      
      return "./images/cards/".$db->getExpansionOfCard($this->id)->internal_name."/".$cardname.$ending;
    }
  }
?>
